//
//  Team9discussViewController.m
//  Team9Project
//
//  Created by dlios on 15/10/13.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "Team9discussViewController.h"
#import "Team9s_discussViewController.h"
#import "Team9disscussCollectionViewCell.h"
#import "Team9BaseView.h"
#import <Masonry.h>
#import "JQAFNetworkTool.h"
#import "Team9MainDisscussModel.h"
#import "YTGetNewData.h"

@interface Team9discussViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate>
@property (nonatomic, strong) UISegmentedControl *seg;
@property (nonatomic, strong) UICollectionView *discussCollectionView;
@property (nonatomic, strong) NSMutableArray *discussArray;
@property (nonatomic, strong) UIScrollView *backView;
// 创建对应的七个数组
@property (nonatomic, strong) NSMutableArray *AllArr;
@property (nonatomic, strong) NSMutableArray *pictureArr;
@property (nonatomic, strong) NSMutableArray *typeArr;
@property (nonatomic, strong) NSMutableArray *designArr;
@property (nonatomic, strong) NSMutableArray *workArr;
@property (nonatomic, strong) NSMutableArray *selectArr;
@property (nonatomic, strong) NSMutableArray *otherArr;

@property (nonatomic, strong) Team9EmptyTool *empty2;
@property (nonatomic, strong) Team9EmptyTool *empty3;
@property (nonatomic, strong) Team9EmptyTool *empty4;
@property (nonatomic, strong) Team9EmptyTool *empty5;
@property (nonatomic, strong) Team9EmptyTool *empty6;
@property (nonatomic, strong) Team9EmptyTool *empty7;


@end

@implementation Team9discussViewController


- (void) viewWillAppear:(BOOL)animated{
    if ([Team9EveningType shareEvening].eveningFailling) {
        self.seg.tintColor = [UIColor colorWithWhite:0.408 alpha:1.000];
        self.discussCollectionView.backgroundColor = [UIColor blackColor];
    }else{
    
        self.seg.tintColor = [UIColor orangeColor];
        self.discussCollectionView.backgroundColor = [UIColor whiteColor];
    }
    
    [self.discussCollectionView reloadData];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.tabBarController.tabBar.translucent = NO;
    
    [self createSegMentControl];
    [self createCollectionView];
    
   
    
}

 #pragma mark 创建seg
- (void)createSegMentControl{
    
    self.backView = [[UIScrollView alloc] init];
    [self.view addSubview:self.backView];
    self.backView.contentSize = CGSizeMake(WIDTH + WIDTH / 5 * 2, 0);
    self.backView.pagingEnabled = NO;
    self.backView.bounces = NO;
    self.backView.showsHorizontalScrollIndicator = NO;
    self.backView.showsVerticalScrollIndicator = NO;
    self.backView.backgroundColor = [UIColor whiteColor];
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.view);
        make.height.mas_equalTo(25 / 375.0 * WIDTH);
        
    }];
    
    
    self.seg =  [[UISegmentedControl alloc] initWithItems:@[@"全部", @"图片提问",@"户型",@"设计", @"施工", @"产品选购", @"其他"]];
    [self.backView addSubview:self.seg];
    [self.seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
     [self.seg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.backView);
        make.height.mas_equalTo(24 / 375.0 * WIDTH);
        make.width.equalTo(self.view.mas_width).multipliedBy(1.4);
    }];
    
    self.seg.selectedSegmentIndex = 0;
    
}


#pragma mark 创建collectionView
- (void)createCollectionView{
    UICollectionViewFlowLayout *flowLayOut = [[UICollectionViewFlowLayout alloc] init];
    flowLayOut.scrollDirection = UICollectionViewScrollDirectionHorizontal; // 横向滚动
    flowLayOut.itemSize = CGSizeMake(WIDTH, HEGHT - 64 - 49 - 25 / 375.0 * WIDTH );
    flowLayOut.minimumInteritemSpacing = 0;
    flowLayOut.minimumLineSpacing = 0;
    
    self.discussCollectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:flowLayOut];
    [self.view addSubview:self.discussCollectionView];
    [self.discussCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.seg.mas_bottom).offset(1 / 375.0 * WIDTH);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(HEGHT - 64 - 49 - 25 / 375.0 * WIDTH);
        
    }];
    
    self.discussCollectionView.bounces=NO;
    self.discussCollectionView.dataSource=self;
    self.discussCollectionView.delegate=self;
    self.discussCollectionView.pagingEnabled=YES;
    
   

    [self.discussCollectionView registerClass:[Team9disscussCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
    


}


#pragma mark seg点击方法
- (void)segAction: (UISegmentedControl *)seg{

    switch (seg.selectedSegmentIndex) {
        case 0:
            [self.discussCollectionView setContentOffset:CGPointZero animated:YES];
            [self.discussCollectionView reloadData];
            break;
        
        case 1:
            [self.discussCollectionView setContentOffset:CGPointMake(WIDTH, 0) animated:YES];
            [self.discussCollectionView reloadData];
            break;
        
        case 2:
            [self.discussCollectionView setContentOffset:CGPointMake(WIDTH * 2, 0) animated:YES];
            [self.discussCollectionView reloadData];
             break;
        
        case 3:
           [self.discussCollectionView setContentOffset:CGPointMake(WIDTH * 3, 0) animated:YES];
            [self.discussCollectionView reloadData];
             break;
            
        case 4:
            [self.discussCollectionView setContentOffset:CGPointMake(WIDTH * 4, 0) animated:YES];
            [self.discussCollectionView reloadData];
            break;
            
        case 5:
            [self.discussCollectionView setContentOffset:CGPointMake(WIDTH * 5, 0) animated:YES];
            [self.discussCollectionView reloadData];
             break;
            
        case 6:
            [self.discussCollectionView setContentOffset:CGPointMake(WIDTH * 6, 0) animated:YES];
            [self.discussCollectionView reloadData];
             break;
        
        default:
            break;
    }
}

#pragma mark 滑动后, seg实现联动
//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView == self.discussCollectionView) {
        
        if (self.discussCollectionView.contentOffset.x < WIDTH) {
            
            self.seg.selectedSegmentIndex = 0;
            [self.backView setContentOffset:CGPointMake(0, 0) animated:YES];
            
        }else if (self.discussCollectionView.contentOffset.x < 2 * WIDTH){
    
            self.seg.selectedSegmentIndex = 1;
            [self.backView setContentOffset:CGPointMake(WIDTH / 5 , 0) animated:YES];
        
        }else if (self.discussCollectionView.contentOffset.x <  WIDTH * 3){
        
              self.seg.selectedSegmentIndex = 2;
            [self.backView setContentOffset:CGPointMake(WIDTH / 5 * 2 , 0) animated:YES];
    
        }else if (self.discussCollectionView.contentOffset.x < WIDTH * 4){

              self.seg.selectedSegmentIndex = 3;

        }else if (self.discussCollectionView.contentOffset.x <  WIDTH * 5){

              self.seg.selectedSegmentIndex = 4;

        }else if (self.discussCollectionView.contentOffset.x < WIDTH * 6){

              self.seg.selectedSegmentIndex = 5;
            
        }else if (self.discussCollectionView.contentOffset.x < WIDTH * 7){

              self.seg.selectedSegmentIndex = 6;
        }

    }
    
   
}


#pragma mark collectionView协议
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{

    return 7;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    Team9disscussCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:reuse forIndexPath:indexPath];
    
    cell.vc = self.vc;
    if (indexPath.row == 0) {
        
        if (!self.AllArr.count) {
            
            cell.tool = [Team9RefreshTool toolWithScrollView:cell.discussTableView url:@"http://api.meilijia.com/app/topic_list?page=1&sort=" style:^NSArray *(id result) {
                
                return [Team9MainDisscussModel baseModelByArr:result[@"data"][@"topic_list"][@"topics"]].mutableCopy;
                
            } refresh:^(NSArray *array) {
                cell.allDataArray = array.mutableCopy;
                self.AllArr = array.mutableCopy;
                
            } empty:cell.empty];
            
        }
        
    }else if(indexPath.row == 1){
        
        if (!self.pictureArr.count) {
            
            

            
            
            cell.tool = [Team9RefreshTool toolWithScrollView:cell.discussTableView url:@"http://api.meilijia.com/app/topic_list?page=1&class_id=9&sort=" style:^NSArray *(id result) {
                
                self.pictureArr = [NSMutableArray array];
                return [Team9MainDisscussModel baseModelByArr:result[@"data"][@"topic_list"][@"topics"]].mutableCopy;
                
                
                
            } refresh:^(NSArray *array) {
                self.pictureArr = array.mutableCopy;
                cell.allDataArray = array.mutableCopy;
//                [self.discussCollectionView reloadData];
                
            } empty:cell.empty];
            
        }
        
    }else if (indexPath.row == 2){
        
        if (self.typeArr.count) {
            
            cell.tool = [Team9RefreshTool toolWithScrollView:cell.discussTableView url:@"http://api.meilijia.com/app/topic_list?page=1&class_id=1&sort=" style:^NSArray *(id result) {
                
                self.typeArr = [NSMutableArray array];
                return [Team9MainDisscussModel baseModelByArr:result[@"data"][@"topic_list"][@"topics"]].mutableCopy;
                
                
                
            } refresh:^(NSArray *array) {
                self.typeArr = array.mutableCopy;
                cell.allDataArray = array.mutableCopy;
//                [self.discussCollectionView reloadData];
                
            } empty:cell.empty];

            
            
//             cell.allDataArray = self.typeArr;
        }
        
    }else if (indexPath.row == 3){
        if (self.designArr) {
            
            
            

            
           cell.tool = [Team9RefreshTool toolWithScrollView:cell.discussTableView url:@"http://api.meilijia.com/app/topic_list?page=1&class_id=3&sort=" style:^NSArray *(id result) {
                
                self.designArr = [NSMutableArray array];
                return [Team9MainDisscussModel baseModelByArr:result[@"data"][@"topic_list"][@"topics"]].mutableCopy;
                
                
                
            } refresh:^(NSArray *array) {
                self.designArr = array.mutableCopy;
                cell.allDataArray = array.mutableCopy;
//                [self.discussCollectionView reloadData];
                
            } empty:cell.empty];
//            cell.allDataArray = self.designArr;
            
        }
        
    }else if (indexPath.row == 4){
        if (self.workArr.count) {
            
           

            
            
           cell.tool = [Team9RefreshTool toolWithScrollView:cell.discussTableView url:@"http://api.meilijia.com/app/topic_list?page=1&class_id=4&sort=" style:^NSArray *(id result) {
                
                self.workArr = [NSMutableArray array];
                return [Team9MainDisscussModel baseModelByArr:result[@"data"][@"topic_list"][@"topics"]].mutableCopy;
                
                
                
            } refresh:^(NSArray *array) {
                self.workArr = array.mutableCopy;
                cell.allDataArray = array.mutableCopy;
//                [self.discussCollectionView reloadData];
                
            } empty:cell.empty];

            
//             cell.allDataArray = self.workArr;
        }
        
    }else if (indexPath.row == 5){
        if (self.selectArr.count) {
            
            

           
            cell.tool = [Team9RefreshTool toolWithScrollView:cell.discussTableView url:@"http://api.meilijia.com/app/topic_list?page=1&class_id=6&sort=" style:^NSArray *(id result) {
                
                self.selectArr = [NSMutableArray array];
                return [Team9MainDisscussModel baseModelByArr:result[@"data"][@"topic_list"][@"topics"]].mutableCopy;
                
                
                
            } refresh:^(NSArray *array) {
                self.selectArr = array.mutableCopy;
                cell.allDataArray = array.mutableCopy;
//                [self.discussCollectionView reloadData];
                
            } empty:cell.empty];

//            cell.allDataArray = self.selectArr;
        }
        
    }else{
        if (self.otherArr.count) {
            
            cell.tool = [Team9RefreshTool toolWithScrollView:cell.discussTableView url:@"http://api.meilijia.com/app/topic_list?page=1&class_id=8&sort=" style:^NSArray *(id result) {
                
                self.otherArr = [NSMutableArray array];
                return [Team9MainDisscussModel baseModelByArr:result[@"data"][@"topic_list"][@"topics"]].mutableCopy;
                
                
                
            } refresh:^(NSArray *array) {
                self.otherArr = array.mutableCopy;
                cell.allDataArray = array.mutableCopy;
//                [self.discussCollectionView reloadData];
                
            } empty:self.empty];

    }
}
#pragma mark collectionView的自定义cell是tableView,实现tableView的点击方法跳转(block)
    
    void(^block)(NSInteger,  NSString * ,NSString *) = ^(NSInteger row , NSString *str ,NSString *strname){
        Team9s_discussViewController *s_discussVC = [[Team9s_discussViewController alloc] init];
        s_discussVC.topicID = str;
        s_discussVC.authorNick = strname;
        
       
        s_discussVC.hidesBottomBarWhenPushed = YES;
        [self.vc.navigationController pushViewController:s_discussVC animated:YES];
        
    };
    
    cell.block = block;
    
    return cell;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}



@end
